package com.lovo.controller;


import com.lovo.bean.RoleInfoBean;
import com.lovo.result.ResponseResult;
import com.lovo.service.IRoleInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.management.relation.RoleInfo;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bdk
 * @since 2021-03-05
 */
@RestController
@RequestMapping("/roles")
@ResponseResult
public class RoleInfoController {
    @Resource
    private IRoleInfoService roleInfoService;
    @GetMapping("/users/{userId}")
    public Object getRoleIdsByUserId(@PathVariable("userId") Long userId){
        return roleInfoService.selectRoleIdsByUserId(userId);
    }
    @GetMapping
    public Object getAllRole(){
        return roleInfoService.list();
    }
    @PostMapping
    public Object add(@RequestBody RoleInfoBean roleInfoBean){
        roleInfoService.save(roleInfoBean);
        return null;
    }
    @DeleteMapping("/{id}")
    public Object delete(@PathVariable("id") Long roleId){
        roleInfoService.deleteById(roleId);
        return null;
    }
    @PutMapping("/{id}")
    public Object update(@PathVariable("id") Long roleId,@RequestBody RoleInfoBean roleInfoBean){
        roleInfoBean.setRoleId(roleId);
        roleInfoService.updateById(roleInfoBean);
        return null;
    }
    /**
     * 给指定角色分配功能
     * @return
     */
    @PostMapping("/{roleId}")
    public Object addUserRole(@PathVariable Long roleId, Long[] funcIds){
        if(funcIds == null){
            funcIds = new Long[0];
        }
        roleInfoService.addFunctionRole(roleId, Arrays.asList(funcIds));
        return null;
    }

}

